#Packages

library("dplyr")
library("tidyverse")
library("plyr")
library("ggthemes")


source("../Paula/Mapping_Applications.R") #Download from 

#Functions

##MinMax
minmax <- function(x, mini=min(x,na.rm = T),maxi=max(x,na.rm=T)){
  
  (x-mini)/(maxi-mini)
  
}

##Inverting scores

reverse_ind <- function(x, lim = 1) {
  lim - x
}

trim_transforms <- function(x, min_val = 0, max_val = 1) {
  x <- pmin(x, max_val)
  pmax(x, min_val)
}

transform_inversion <- function(x) {
  x <- reverse_ind(x)
  trim_transforms(x)
}

#Datasets

load(file="Data/Existent Data on Religion/Treated Data/Final Data/accommin.rdata") #Dataset used for the Indicator building process - Religious Accommodation Indicator

#Summing 

AccomMin$Sum <- rowSums( AccomMin[,3:10] )
AccomMin <- AccomMin %>% mutate_if(is.numeric, round, digits=2)

#Calculating Indicator

AccomMin$Accommodation <- minmax(AccomMin$Sum)
AccomMin <- AccomMin %>% mutate_if(is.numeric, round, digits=2)

#Inverting Values for Indicator

AccomMin$Accommodation <- transform_inversion(AccomMin$Accommodation)

#Setting up data for maps

rel$Sum <- rowSums(rel[,3:10])
rel$Accommodation <- minmax(rel$Sum)
rel <- rel %>% mutate_if(is.numeric, round, digits=2)
ac <- rel[,c("Country", "Accommodation")]
CountryCode <- c("AT", "BE", "BG", "HR", "CZ", "DK", "EE", "FI", "FR", "DE", "EL", "HU", "IE", "IT", "LV", "LT", "LU", "MT", "NL", "PL", "PT", "RO", "SK", "SI", "ES", "SE", "UK")
ac$CountryCode <- CountryCode

ac <- ac %>%
  arrange(CountryCode)

ac$Accommodation <- transform_inversion(ac$Accommodation)

#EU Map

eurMap <- readShapeSpatial("Plots/Maps/NUTS_RG_01M_2013_4326/NUTS_RG_01M_2013_4326.shp")
eu <- eurMap
eu$CountryCode <- eu$CNTR_CODE

eu <- merge(eu, ac, by="CountryCode")

eu <- eu[!eu$CountryCode%in%c("CY",'IS','TR'),]
par(mar=rep(0,4))

map(eu, val = eu$Accommodation, custcol = c("#ffffff","#ffd4e4","#ffa7ca","#ff76b0","#fa3497"), xlim = c(-11.37367,31.34688), ylim=c(32.67620,71.95155), legpos = "topleft", y.intersp = 0.75, title = "Fig 7. Religious Accommodation in the European Union", leg.main = "Accomodation", na.col = "grey80")


#Multidumbell

Accom <- AccomMin[,c("Country", "Group", "Accommodation")]
Accom$Group <- revalue(Accom$Group, c("Jehova's Witnesses" = "Jehovah's Witnesses"))
Accom$Group <- revalue(Accom$Group, c("All" = "Country Score"))

a <- Accom[Accom$Group=="Country Score",]
a <- a[order(a$Accommodation),]

Accom$Country  <- as.character(Accom$Country)
Accom$Country <- factor(as.character(Accom$Country), levels= as.character(a$Country), ordered = T)

group <- c("Country Score","Jews","Jehovah's Witnesses","Orthodox","Protestants","Catholics","Muslims")

coma <- Accom[Accom$Group%in%c(group),]

un <- 1/(length(group))

for(i in 1:length(group)){
  coma$size[coma$Group==group[i]] <- i*(un)
}

coma <- coma[order(coma$Country, coma$size, decreasing = T),]
coma$Minority <- coma$Group


p <- ggplot(coma, aes(Accommodation, Country)) +
  geom_line(color="#FFE9F1") +
  geom_line(data=a, aes(x=Accommodation, y=c(1:nrow(a))), color="black", size=0.4) +
  geom_point(aes(color = Minority, size=size)) +
  scale_size_continuous(range = c(1,6.5)) +
  labs(title = "Fig.8 Religious Accommodation in the European Union",
       subtitle = "Comparison between National Accommodation Policy (Country-level Score) vs Specific Minority Accommodation (Scores for Selected Minorities). \n(The gap represents the difference between the national policy for all minorities and special agreements with individual minorities.)") +
  xlab("Accommodation Score") +
  theme(axis.title = element_blank(),
        axis.text.x = element_text(size=12),
        axis.text.y = element_text(size=12),
        panel.grid.major.x = element_blank(),
        panel.grid.minor = element_blank(),
        legend.title = element_blank(),
        legend.justification = c(0, 1), 
        legend.position = c(.5, 1.13),
        legend.background = element_blank(),
        legend.direction="horizontal",
        legend.text= element_text(size=12),
        plot.title = element_text(size = 18, margin = margin(b = 10)),
        plot.subtitle = element_text(size = 14, color = "darkslategrey", margin = margin(b = 25)),
        plot.caption = element_text(size = 8, margin = margin(t = 10), color = "grey70", hjust = 0)) +
  guides(color = guide_legend(override.aes = list(size=3)))+
  guides(size="none") +
  scale_color_manual(labels = c("Catholics","Country Score", "Jehovah's Witnesses", "Jews", "Muslims", "Orthodox", "Protestants"), values=c("purple", "black", "pink", "orange", "steelblue",  "brown", "yellow"))+
  guides(size="none")+
  labs(caption="Source: Religious Accommodation Indicator. Own Elaboration.") +
  theme_classic()+
  theme(legend.position = c(0.15,0.75))

p 

#Map Islam

eu <- eurMap
eu$CountryCode <- eu$CNTR_CODE

eu <- merge(eu, Mus, by="CountryCode")

eu <- eu[!eu$CountryCode%in%c("CY",'IS','TR'),]
par(mar=rep(0,4))

map(eu, val = eu$Accommodation, custcol = c("#ffffff","#ffd4e4","#ffa7ca","#ff76b0","#fa3497"), xlim = c(-11.37367,31.34688), ylim=c(32.67620,71.95155), legpos = "topleft", y.intersp = 0.75, title = "Fig. 9 Religious Accommodation of Muslims in the European Union", leg.main = "Accomodation", na.col = "grey80")

#Map Judaism

eu <- eurMap
eu$CountryCode <- eu$CNTR_CODE

eu <- merge(eu, Jew, by="CountryCode")

eu <- eu[!eu$CountryCode%in%c("CY",'IS','TR'),]
par(mar=rep(0,4))

map(eu, val = eu$Accommodation, custcol = c("#ffffff","#ffd4e4","#ffa7ca","#ff76b0","#fa3497"), xlim = c(-11.37367,31.34688), ylim=c(32.67620,71.95155), legpos = "topleft", y.intersp = 0.75, title = "Fig. 10 Religious Accommodation of Jews in the European Union", leg.main = "Accomodation", na.col = "grey80")
